package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.ContentListAdapter;
import com.youmingxing.adpter.selfInfo.ChooseAuditionsListAdapter;
import com.youmingxing.bean.Content;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : 李雄
 * Date : 2016/4/5 0005
 * Email: LiXiong0713@163.com
 * Description :
 * 选择置顶作品
 */
public class ChooseMyAuditionsActivity extends BaseActivity implements
        RefreshListView.OnLoadMoreListener, View.OnClickListener {

    private TextView tv_next;

    private String cardno;// 存放使用的置顶券的券号
    private int update_id;// 存放置顶的作品ID
    private String tips;// 置顶时间

    private ChooseAuditionsListAdapter adapter;
    private RefreshListView listView;
    private List<Content> contentList;
    int mark = 1;
    int userId = 1;
    int type = 0;
    String url;
    public final static int TYPE_USER_WORK = 0; //我的作品
    public final static int TYPE_GREAT_WORK = 1; //优秀作品
    public final static String EXTRAS_ID = "u_id";
    public final static String EXTRAS_TYPE = "type";
    public final static String EXTRAS_URL = "url";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_choose_my_auditons);
        contentList = new ArrayList<>();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            userId = bundle.getInt(EXTRAS_ID, -1);
            type = bundle.getInt(EXTRAS_TYPE, TYPE_USER_WORK);
            url = bundle.getString(EXTRAS_URL, "");
        }
        initView();
        if (type == TYPE_USER_WORK) {
            AllStarApi.getUserContents(userId, mark, contentHandler);
        } else if (type == TYPE_GREAT_WORK) {
            AllStarApi.getGreateContent(url, -1, -1, contentHandler);
        }
    }

    @Override
    public void onErrorClick() {
        AllStarApi.getUserContents(userId, mark, contentHandler);
    }

    private void initView() {
        setTopbarName(R.string.title_choose_activity_my_work);
        listView = (RefreshListView) findViewById(R.id.list_choose_my_work);
        tv_next = (TextView) findViewById(R.id.tv_next);
        tv_next.setOnClickListener(this);
        listView.setOnLoadMoreListener(this);
        adapter = new ChooseAuditionsListAdapter(this);
        listView.setAdapter(adapter);

        cardno = getIntent().getStringExtra("cardno");
        tips = getIntent().getStringExtra("tips");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    BaseJsonHandler contentHandler = new BaseJsonHandler(this) {
        public void onSuccessWithData(JSONObject response) {
            listView.endLoadMore();
            showErrorPage(false);
            JSONArray data = response.optJSONArray("data");
            if (mark == 1 && data.length() == 0) {
                listView.noData("您当前还没有作品哦");
            }
            mark++;
            if (data.length() < 20) {
                listView.noMoreData();
            }
            for (int i = 0; i < data.length(); i++) {
                Content content = new Content(data.optJSONObject(i));
                contentList.add(content);
            }
            adapter.setData(contentList);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            listView.endLoadMore();
            showErrorPage(true);
        }
    };

    @Override
    public void onLoadMore() {
        if (type == TYPE_USER_WORK) {
            AllStarApi.getUserContents(userId, mark, contentHandler);
        } else {
            Content lastItem = contentList.get(contentList.size() - 1);
            AllStarApi.getGreateContent(url, lastItem.getCreatetime(), lastItem.getId(),
                    contentHandler);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        ChooseAuditionsListAdapter.selectedAuditionsId = -1;
        if ( ChooseAuditionsListAdapter.mCheckBox != null) {
            ChooseAuditionsListAdapter.mCheckBox.setChecked(false);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, ChooseTimeActivity.class);
        update_id = ChooseAuditionsListAdapter.selectedAuditionsId;
        String url = null;
        for (Content content : contentList) {
            if (content.getId() == update_id) {
                url = content.getThumbnail();
            }
        }
        intent.putExtra("cardno", cardno);
        intent.putExtra("url", url);
        intent.putExtra("tips", tips);
        startActivity(intent);
    }
}
